package sxr.common.aspect;

import cn.thinkjoy.springboot.vuc.facade.ICommonFacade;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by xufei on 2019/11/26.
 */
@Aspect
@Component
public class DaoLogAspect {
    private static final Logger logger = LoggerFactory.getLogger(DaoLogAspect.class);

    @Autowired
    private ICommonFacade commonFacade;

    @Pointcut("execution( * cn.thinkjoy.springboot.vuc.service.*.add*(..))||execution( * cn.thinkjoy.springboot.vuc.service.*.update*(..))||execution( * cn.thinkjoy.springboot.vuc.service.*.del*(..))")//两个..代表所有子目录，最后括号里的两个..代表所有参数
    public void daoLogPointCut() {
    }

    @Around("daoLogPointCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object ob = pjp.proceed();// ob 为方法的返回值
        logger.info("耗时 : " + (System.currentTimeMillis() - startTime));
        return ob;
    }

    @AfterReturning(returning = "retValue", pointcut = "daoLogPointCut()")// returning的值和doAfterReturning的参数名一致
    public void doAfterReturning(JoinPoint joinPoint, Object retValue) throws Throwable {
        commonFacade.addDataOperationRecord(joinPoint,retValue);
    }
}
